(function(app){
	app
		.controller("content_03_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$state","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$state,$q,$ionicHistory){
			//接收参数
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var searchParam = $stateParams.searchParam;
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var signroleFlag; //变更对象标志 1-投保人、2-被保险人、3-两者
			var type1Flag = false;//投保人勾选标志
			var type2Flag = false;////被保险人勾选标志
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var token=sessionStorage.getItem("token");
			
			//绑定参数到页面
			$scope.chdrnum01 = $stateParams.chdrnum01;
			$scope.readFlag = false;//阅读协议标志，默认为false
			
/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						var item03 = itemData.item03;
						var signrole;
						if(item03!=null && item03!=undefined && item03!=""){
							signrole = item03.signrole;
						}
						
						//选框初始化
						if(signrole!=undefined && signrole!=null && signrole!=""){//不会空时才做以下操作
							if(signrole == "1"){
								$("#s11").attr("checked","checked");
								$("#s12").removeAttr("checked");
								type1Flag = true;
								type2Flag = false;
							}else if(signrole == "2"){
								$("#s11").removeAttr("checked");
								$("#s12").attr("checked","checked");
								type1Flag = false;
								type2Flag = true;
							}else if(signrole == "3"){
								$("#s11").attr("checked","checked");
								$("#s12").attr("checked","checked");
								type1Flag = true;
								type2Flag = true;
							}
						}
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数begin****************************************************/
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回函数
			$scope.goBack = function () {
				myFactory.goBack();
			}
			//客户须知弹框
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn03Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn03Input');
			}
			//改变已阅读标志
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn03Input','overwindowOn03Input','policyCloseXOn03Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;//绑定至页面
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn03Input = function(){
				modal('close','policyModalOn03Input','overwindowOn03Input','policyCloseXOn03Input');
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			
			$scope.chooseType = function(type){
				if(type=="1"){
					type1Flag = !type1Flag;
				}
				if(type=="2"){
					type2Flag = !type2Flag;
				}
//				console.log("投保人："+type1Flag);
//				console.log("被保人："+type2Flag);
			}
			
			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				if($scope.readFlag == false){
					myFactory.alert("","请先同意《签名变更人声明》!","info");
					return;
				}
				if(type1Flag==false && type2Flag==false){
					myFactory.alert("","请选择变更内容!","info");
					return;
				}else if(type1Flag==true && type2Flag==false){
					signroleFlag = "1";
				}else if(type1Flag==false && type2Flag==true){
					signroleFlag = "2";
				}else if(type1Flag==true && type2Flag==true){
					signroleFlag = "3";
				}
				
				$ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:signroleFlag,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
					
				
				
			}
		}]); 
	
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)